package com.xps.and.yuntong.Ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jude.utils.JUtils;
import com.xps.and.yuntong.Data.bean1.ConfirmSettlement;
import com.xps.and.yuntong.Data.bean1.OrderSettlement;
import com.xps.and.yuntong.Data.net.UserNetWorks;
import com.xps.and.yuntong.R;
import com.xps.and.yuntong.Utils.AppCommond;
import com.xps.and.yuntong.base.BaseActivity;

import org.greenrobot.eventbus.EventBus;

import rx.Subscriber;

/**
 * Created by administrator on 2017-12-15.
 */

public class DingdanJiesuanActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_jiesuan_fanhui, iv_jiesuan_right;
    private TextView tv_middle, tv_licheng1, tv_feiyong_xingshi1, tv_yongdu1, tv_feiyong_yongdu1, tv_dengdai1, tv_feiyong_dengdai1, tv_feiyong_zong1;
    private LinearLayout ll_feiyong_next;


    private String order_tiaozhuan;
    //结算用到的参数
    private String congestionMoney;
    private String waitMoney;
    private String kmMoney;
    private String totalMoney;
    private String congestionTime;
    private String waitTime;
    private String km;
    private String driver_order_id;

    @Override
    protected int getLayout() {
        return R.layout.activity_dingdan_jiesuan;
    }

    @Override
    protected void setData() {

    }

    @Override
    protected void init(Bundle inBundle) {
        initView();
    }

    private void initView() {

        iv_jiesuan_fanhui = (ImageView) findViewById(R.id.iv_fanhui);
        iv_jiesuan_right = (ImageView) findViewById(R.id.iv_right);

        tv_middle = (TextView) findViewById(R.id.tv_middle);
        tv_middle.setText(R.string.dingdan_jiesuan);
        tv_licheng1 = (TextView) findViewById(R.id.tv_licheng1);
        tv_feiyong_xingshi1 = (TextView) findViewById(R.id.tv_feiyong_xingshi1);
        tv_yongdu1 = (TextView) findViewById(R.id.tv_yongdu1);
        tv_feiyong_yongdu1 = (TextView) findViewById(R.id.tv_feiyong_yongdu1);
        tv_dengdai1 = (TextView) findViewById(R.id.tv_dengdai1);
        tv_feiyong_dengdai1 = (TextView) findViewById(R.id.tv_feiyong_dengdai1);
        tv_feiyong_zong1 = (TextView) findViewById(R.id.tv_feiyong_zong1);

        ll_feiyong_next = (LinearLayout) findViewById(R.id.ll_feiyong_next);

        iv_jiesuan_fanhui.setOnClickListener(this);
        iv_jiesuan_right.setOnClickListener(this);
        ll_feiyong_next.setOnClickListener(this);

        Intent intent = getIntent();
        order_tiaozhuan = intent.getStringExtra("order_tiaozhuan");
        driver_order_id = getIntent().getStringExtra("driver_order_id");
        if (order_tiaozhuan.equals("0")) {
            congestionMoney = intent.getStringExtra("congestionMoney");
            waitMoney = intent.getStringExtra("waitMoney");
            kmMoney = intent.getStringExtra("kmMoney");
            totalMoney = intent.getStringExtra("totalMoney");
            congestionTime = intent.getStringExtra("congestionTime");
            waitTime = intent.getStringExtra("waitTime");
            km = intent.getStringExtra("km");

            tv_feiyong_xingshi1.setText(kmMoney);
            tv_feiyong_yongdu1.setText(congestionMoney);
            tv_feiyong_dengdai1.setText(waitMoney);
            tv_feiyong_zong1.setText(totalMoney);
            tv_licheng1.setText(km);
            tv_yongdu1.setText(congestionTime);
            tv_dengdai1.setText(waitTime);
        } else if (order_tiaozhuan.equals("1")) {

            orderSettlement();

        }

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.iv_fanhui:
                finish();

                break;
            case R.id.iv_right:
                finish();

                break;
            case R.id.ll_feiyong_next:
                confirmSettlement();
                break;


        }
    }


    private void confirmSettlement() {
        UserNetWorks.confirmSettlement("346", new Subscriber<ConfirmSettlement>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                JUtils.Toast("网络错误");
            }

            @Override
            public void onNext(ConfirmSettlement confirmSettlement) {
                if (confirmSettlement.getReturn_code().equals("SUCCESS")) {

                    finish();

                } else if (confirmSettlement.getReturn_code().equals("FAIL")) {

                    finish();


                }
            }
        });

    }


    private void orderSettlement() {

        //346 需要替换成driver_order_id
        UserNetWorks.orderSettlement("346", new Subscriber<OrderSettlement>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                JUtils.Toast("网络错误");
            }

            @Override
            public void onNext(OrderSettlement orderSettlement) {

                if (orderSettlement.getReturn_code().equals("SUCCESS")) {

                    tv_feiyong_xingshi1.setText(orderSettlement.getReturn_body().getKmMoney());
                    tv_feiyong_yongdu1.setText(orderSettlement.getReturn_body().getCongestionMoney());
                    tv_feiyong_dengdai1.setText(orderSettlement.getReturn_body().getWaitMoney());
                    tv_feiyong_zong1.setText(orderSettlement.getReturn_body().getTotalMoney());
                    tv_licheng1.setText(orderSettlement.getReturn_body().getKm());
                    tv_yongdu1.setText(String.valueOf(orderSettlement.getReturn_body().getCongestionTime()));
                    tv_dengdai1.setText(String.valueOf(orderSettlement.getReturn_body().getWaitTime()));


                } else if (orderSettlement.getReturn_code().equals("FAIL")) {

                    JUtils.Toast(orderSettlement.getReturn_msg());

                }
            }
        });


    }


}
